#include "IPlocation.h"
IPlocation iplocation;



//解析Json信息
void get_ip_json(String payload)
{
  char *p=&payload[0];
  p+=233;
  while(*p++)
  {
    if(*p=='p'&&*(p+1)=='o')
    {
      p+=13;
      break;
    }
  }
  iplocation.longitude=*p++;
  while(*p)
  {
    if(*p!='"')
      iplocation.longitude+=*p++;
    else
    {
      p+=7;
      break;
    }
      
  }
  
   iplocation.latitude=*p++;
  while(*p)
  {
    if(*p!='"')
      iplocation.latitude+=*p++;
    else
    {
      break;
    }
      
  }
  #ifdef DEBUG
  {
    Serial.print("longitude=");
    Serial.println(iplocation.longitude);
    Serial.print("latitude=");
    Serial.println(iplocation.latitude);
  }
  #endif
}

bool IPlocation::get()
{
  #ifdef DEBUG
  {
    Serial.println("start get IP location");
  }
  #endif
  String dat = http_get(IP_location_url,0);
  if(strlen(&dat[0])>100)
  {
    #ifdef DEBUG
    {
        Serial.println("get success");
        Serial.print("code=");
        Serial.println(dat);
    }
    #endif
    get_ip_json(dat);
    return 1;
  }
  else
  {
    #ifdef DEBUG
    {
      Serial.println("get fail");
    }
    #endif
    return 0;
  }

  
}
